package cn.net.xyan.image;

import com.sun.jna.Native;
import com.sun.jna.Platform;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zarra on 2017/1/26.
 */
public class NativeLibraryLoad {

    static Map<String, File> libraryCache = new HashMap<>();

    public static String nativeLibPath() {
        String nativeLibPath = "";
        if (Platform.isWindows()) {
            if (Platform.is64Bit()) {
                nativeLibPath = "lib";
            } else {
                nativeLibPath = "lib";
            }
        } else if (Platform.isMac()) {
            nativeLibPath = "";
        }

        return nativeLibPath;
    }

    public static File extractFromJar(String lib) {
        String realLib = nativeLibPath() + System.mapLibraryName(lib);

        File file = libraryCache.get(lib);
        try {
            if (file == null) {
                file = Native.extractFromResourcePath(realLib);

                libraryCache.put(realLib, file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return file;
    }
}